package com.cyss.greenball.view;

import java.util.List;
import com.cyss.greenball.world.Hole;
import com.lianzt.commondata.AbstractCommonData;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class MyMapViewFactory {
	public static Bitmap getBitmapByCommdata(int width, int height,
			AbstractCommonData acd) {
		Bitmap bitMap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitMap);
		canvas.drawColor(Color.GRAY);
		Paint paint = new Paint();
		AbstractCommonData head = acd.getDataValue("head");
		int fromScreenHeight = head.getIntValue("screenHeight");
		int fromScreenWidth = head.getIntValue("screenWidth");
		List<AbstractCommonData> lineBarrierList = acd
				.getArrayValue("lineBarrier");
		List<AbstractCommonData> recBarrierList = acd
				.getArrayValue("recBarrier");
		List<AbstractCommonData> holeList = acd.getArrayValue("hole");
		List<AbstractCommonData> greenBallList = acd.getArrayValue("greenBall");
		float scaleX = (float) width / (float) fromScreenWidth;
		float scaleY = (float) height / (float) fromScreenHeight;
		paint.setColor(Color.RED);
		for (AbstractCommonData acdLine : lineBarrierList) {
			float startX = acdLine.getFloatValue("startX") * scaleX;
			float startY = acdLine.getFloatValue("startY") * scaleY;
			float endX = acdLine.getFloatValue("endX") * scaleX;
			float endY = acdLine.getFloatValue("endY") * scaleY;
			canvas.drawLine(startX, startY, endX, endY, paint);
		}

		for (AbstractCommonData acdRec : recBarrierList) {
			float left = acdRec.getFloatValue("left") * scaleX;
			float top = acdRec.getFloatValue("top") * scaleY;
			float right = acdRec.getFloatValue("right") * scaleX;
			float bottom = acdRec.getFloatValue("bottom") * scaleY;
			canvas.drawRect(left, top, right, bottom, paint);
		}
		paint.setStyle(Paint.Style.STROKE);
		for (AbstractCommonData acdHole : holeList) {
			float centerX = acdHole.getFloatValue("centerX") * scaleX;
			float centerY = acdHole.getFloatValue("centerY") * scaleY;
			int holeType = acdHole.getIntValue("holeType");
			switch (holeType) {
			case Hole.TRAP:
				paint.setColor(Color.BLACK);
				break;
			case Hole.DESTINY:
				paint.setColor(Color.BLUE);
				break;
			default:
				break;
			}
			canvas.drawCircle(centerX, centerY, width / 22, paint);
		}
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.GREEN);
		for (AbstractCommonData acdGreenBall : greenBallList) {
			float centerX = acdGreenBall.getFloatValue("centerX") * scaleX;
			float centerY = acdGreenBall.getFloatValue("centerY") * scaleY;
			canvas.drawCircle(centerX, centerY, width / 22, paint);
		}
		return bitMap;
	}
}
